// 设置默认信息
$.validator.setDefaults({
    submitHandler: function (val) {
        oktipsoption = {
            type: 1,
            url: $('.formsub').attr('url'),
            msg: '密码重置成功',
        };
        ajaxform.init(oktipsoption, '.formsub');
        ajaxform.submit();
    },
    errorPlacement: function (error, element) {
        element.parents('.l_b').append(error);
        element.parents('.l_b').find('.prompt').hide();
    }
});


$(document).ready(function () {

    $(".l_b input").focus(function () {
        $(this).addClass('u-ipt-active');
        $(this).removeClass('error');
        $(this).next('.error').hide();
    });


    $(".l_b input").blur(function () {
        $(this).removeClass('u-ipt-active');
        if ($(this).parents('.l_b').find('label.error').is(":visible")) {
            $(this).addClass('error');
            $(this).parents('.l_b').find('.prompt').hide();
        } else {
            $(this).removeClass('error');
            $(this).removeClass('u-ipt-active');
            $(this).parent('.file_name').removeClass('error');
             $(this).parents('.l_b').find('.prompt').show();
        }
    });

    // 验证邮箱验证码
    $.validator.addMethod("chkemailcode", function (value) {
        var emailcodeReg = /^\d{6}$/;
        return emailcodeReg.test(value);
    });

    // 验证密码
    $.validator.addMethod("chkpassword", function (value) {
        var pwdReg = /[^\u4e00-\u9fa5]+/;
        return pwdReg.test(value);
    });

    $("#email_form").validate({
        rules: {
            email: {
                required: true,
                email: true,
                async: false,
                remote: {
                    url: $("#checkemail").val(),
                    type: "Post",
                    data: {
                        email: function () {
                            return $("#email").val();
                        }
                    },
                    dataType: "json"
                }
            },
            emailcode: {
                required: true,
                chkemailcode: true,
                remote: {
                    url: $("#checkemailcode").val(),
                    type: "Post",
                    data: {
                        emailcode: function () {
                            return $("#emailcode").val();
                        }
                    },
                    dataType: "json"
                }
                // emailcodeck: true
            },
            password: {
                required: true,
                minlength: 6,
                maxlength: 12,
                chkpassword: true
            },
            repassword: {
                required: true,
                equalTo: "#password"
            }
        },
        messages: {
            email: {
                required: '请输入邮箱',
                email: '邮箱地址不正确',
                remote: '邮箱不存在'
            },
            emailcode: {
                required: '请输入邮箱验证码',
                chkemailcode: '验证码为6位数字',
                remote: '请输入正确的邮箱验证码'
                        // emailcodeck:'请输入正确的邮箱验证码'
            },
            password: {
                required: '请输入密码',
                minlength: '至少输入6位非中文密码',
                maxlength: '最多12位非中文密码',
                chkpassword: '请输入6-12位非中文密码'
            },
            repassword: {
                required: '请再次输入密码',
                equalTo: "两次密码输入不一致"
            }
        },
        focusInvalid: false,
        onkeyup: false
    });
});

// 短信验证计时
var InterValObj2; //timer变量，控制时间
var curCount2;//当前剩余秒数
function sendMessage2() {
    var count = 60; //间隔函数，1秒执行
    if ($("#email_form").validate().element($("#email"))) {
        curCount2 = count;
//设置button效果，开始计时
        $(".js_mailcode").attr("disabled", "true");
        $(".js_mailcode").text(curCount2 + "S");
        $(".js_mailcode").css("background", "#ccc");
         $(".js_mailcode").removeAttr("onclick",'');
        InterValObj2 = setInterval(SetRemainTime2, 1000); //启动计时器，1秒执行一次
        //向后台发送处理数据
        $.ajax({
            type: "POST", //用POST方式传输
            dataType: "json", //数据格式:JSON
            url: $('#sendemailcode').val(), //目标地址
            data: {email: $("#email").val()}
        });
    }
}

//timer处理函数
function SetRemainTime2(obj) {
    if (curCount2 == 0) {
        clearInterval(InterValObj2);//停止计时器
        $(".js_mailcode").removeAttr("disabled");//启用按钮
         $(".js_mailcode").attr("onclick",'sendMessage2()');
        $(".js_mailcode").text("重新发送");
        $(".js_mailcode").css("background", "#f60");
    } else {
        curCount2--;
        $(".js_mailcode").text(curCount2 + "S");
    }
}